<template>
	<view class="ts-column ts-flex-item">
		<ts-banner :image="config.cover_image" height="250"></ts-banner>

		<view class="ts-row">
			<view class="ts-row ts-flex-item" @tap="actionSheetStoreTypeTap">
				<ts-button type="primary">{{storeType}}</ts-button>
			</view>
			<view class="ts-row ts-flex-item">
				<ts-button type="success">全部类型</ts-button>
			</view>
			<view class="ts-row ts-flex-item">
				<ts-button type="danger">全部类型</ts-button>
			</view>
		</view>

		<ts-search-bar v-model="keywords" @search="doSearch"></ts-search-bar>

		<view class="ts-column ts-flex-item">
			<view v-for="(item,index) in list" :key="index" class="ts-column ts-flex-item">
				<view class="ts-column ts-padding">
					<image :src="item.cover_image" style="width: 100%; height: 250upx;"></image>
					<view class="ts-row" style="justify-content: space-between;">
						<text class="ts-h3">{{item.title}}</text>
						<view>
							<ts-badge type="danger" :text="item.type"></ts-badge>
						</view>
					</view>
					<view class="ts-row">
						<text>{{item.open_time}}</text>
					</view>
					<view class="ts-row">
						<text>{{item.service}}</text>
					</view>
					<view class="ts-row">
						<text>{{item.address}}</text>
					</view>
				</view>
				<view class="ts-gap"></view>
			</view>


		</view>

		<view class="ts-column">
			<ts-load-more :loadingType="loadingType" :contentText="contentText" @tap="getStoreListMoreData"></ts-load-more>
		</view>

	</view>
</template>

<script>
	import tsBanner from 'components/teaset/components/ts-banner.vue';
	import tsSearchBar from 'components/teaset/components/ts-search-bar.vue'

	var self;
	export default {
		components: {
			tsBanner, //ts-banner
			tsSearchBar,
		},
		data: {
			storeType:'全部类别',
			keywords: '',
			list: [],
			config: {},
			page:0,
			loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
		},
		onLoad() {
			self = this;
			this.getAppConfig();
			this.getStoreListMoreData();
		},
		onReachBottom() {
			this.getStoreListMoreData();
		},
		methods: {
			doSearch() {
				this.list=[];
				this.page=0;
				this.getStoreListMoreData();
			},
			getAppConfig() {
				const api = this.$API_BASE + '/app/store/getAppConfig';
				uni.request({
					url: api,
					method: 'GET',
					dataType: 'json',
					data: {},
					success(response) {
						const res = response.data;
						console.log(JSON.stringify(res));
						// 						{
						// 							success:true,
						// 							message:'',
						// 							data:{}
						// 						}

						if (res.success) {
							self.config = res.data;
						} else {
							console.log(res.message);
						}
					}
				})
			},
			getStoreListMoreData() {
				
				if (this.loadingType!=0){
					return;
				}
				
				this.page=this.page+1;
				this.loadingType=1;
				
				const api = this.$API_BASE + '/app/store/getStoreList';
				uni.request({
					url: api,
					method: 'GET',
					dataType: 'json',
					data: {
						keywords:self.keywords,
						page:self.page,
						type:self.storeType,
					},
					success(response) {
						const res = response.data;

						console.log(JSON.stringify(res));
						// 						{
						// 							success:true,
						// 							message:'',
						// 							has_more:true,
						// 							data:[]
						// 						}

						if (res.success) {
							self.list = self.list.concat(res.data);
							
							if (res.has_more){
								self.loadingType=0;
							}else{
								self.loadingType=2;
							}
						} else {
							console.log(res.message);
						}
					}
				})
			},
		
		actionSheetStoreTypeTap() {
			const itemList = ['全部类别', '水果店', '菜篮子', '士多店', '快餐店', '其它'];
							uni.showActionSheet({
								itemList: itemList,
								success: e => {
									// console.log(JSON.stringify(e));
									self.storeType = itemList[e.tapIndex];
									self.doSearch();
								}
							});
			}
		}

	}
</script>

<style>
	page view {
		/* 		border: #000000 solid 1px;
		margin: 5upx; */
	}

	.ts-column {
		display: flex;
		flex-direction: column;
	}

	.ts-flex-item {
		flex: 1;
	}
</style>
